package com.ruoyi.Utils;

import java.util.Random;

public class ProbabilityUtil {
    // 定义1-6级物品的概率权重，等级越高权重越小
    // 这里的权重比例可以根据需求调整，总和不影响概率分配
//    private static final int[] WEIGHTS = {0, 0, 30, 30, 30, 10};
    private static final int[] WEIGHTS = {0, 0, 0, 0, 0, 100};
    private static final Random random = new Random();

    /**
     * 随机获取1-6级物品，等级越高概率越低
     * @return 物品等级(1-6)
     */
    public static int getRandomItemLevel() {
        // 计算权重总和
        int totalWeight = 0;
        for (int weight : WEIGHTS) {
            totalWeight += weight;
        }

        // 生成0到总权重之间的随机数
        int randomValue = random.nextInt(totalWeight);

        // 根据随机数落在的权重区间判断物品等级
        int cumulativeWeight = 0;
        for (int i = 0; i < WEIGHTS.length; i++) {
            cumulativeWeight += WEIGHTS[i];
            if (randomValue < cumulativeWeight) {
                // 返回等级(索引+1，因为等级从1开始)
                return i + 1;
            }
        }

        // 理论上不会走到这里，默认返回最低等级
        return 1;
    }

    /**
     * 随机获取1-4个物品
     */
    public static int getRandomNum() {
        return random.nextInt(4) + 1;
    }
}
